home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11996 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  69 lines

  1. Path: news1.erols.com!newsmaster@erols.com
  2. From: Chris Cobb <ccobb@erols.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How to get at base class fields from member functions?
  5. Date: 17 Mar 1996 17:20:15 GMT
  6. Organization: CSEG, Inc.
  7. Message-ID: <4ihhkf$2ni@news5.erols.com>
  8. References: <internews46B00D1FBD@argonet.co.uk>
  9. NNTP-Posting-Host: ccobb.erols.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22KIT (Windows; U; 16bit)
  14.  
  15. Dave Mullard <dmullard@argonet.co.uk> wrote:
  16. >Given three classes A, B and C I want to have multiple As for each B and
  17. >multiple Bs for each C. To do this I could create the following.
  18. >
  19. >class b1 : public B
  20. >{
  21. >A a1;
  22. >A a2;
  23. >A a3;
  24. >};
  25. >
  26. >class b2 : public B
  27. >{
  28. >A a1;
  29. >A a2;
  30. >A a3;
  31. >A a4;
  32. >A a5;
  33. >};
  34. >
  35. >class c1 : public C
  36. >{
  37. >b1 b1;
  38. >b2 b2;
  39. >};
  40. >
  41. >The question is, how within functions for class B do I access fields
  42. >within class C, and similarly, how within the class A functions do I
  43. >access fields within class B?
  44. >
  45. >Any help would be appreciated.
  46. >
  47.  
  48. You have encountered the wall of encapsulation.  It is considered poor 
  49. form to violate this wall without good reason.  The need to violate this 
  50. wall may indicate poor design.  However, C++ is not so rigid as to 
  51. disallow this.  The way this is done is to have the class whose members 
  52. need to be accessed declare the class who wants to access them a friend:
  53.  
  54. class C
  55. {
  56.    friend class B;
  57.    // ...
  58. };
  59.  
  60. class B
  61. {
  62.    friend class A;
  63.    // ...
  64. };
  65.  
  66. Chris
  67. ccobb@cseg.com
  68.  
  69.